package com.forum.action;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import sun.misc.BASE64Encoder;

import com.forum.dao.UserDAO;
import com.forum.pojo.User;
import com.forum.util.BaseAction;
import com.opensymphony.xwork2.ActionContext;

@Component("userAction")
@Scope("prototype")
public class UserAction extends BaseAction{
	private User user;
	private String userName;
	private String userpass;
	private String useremail;
	private String sex;
	private UserDAO userDAO;
	private int userId;
	private String birthday;
	private String headImage;
	private String usertype;
	private String regdate;
	Calendar cal=Calendar.getInstance();
	@Resource
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
	/**
	 * 
	  * EncoderPwdByMd5 方法 
	  * <p>方法说明:</p> 
	  * @param str
	  * @return
	  * @throws NoSuchAlgorithmException
	  * @throws UnsupportedEncodingException 
	  * @return String 
	  * @author Evan 
	  * @date 2012-12-17
	 */
	public String EncoderPwdByMd5(String str) throws NoSuchAlgorithmException, 
	UnsupportedEncodingException { 
		// 确定计算方法 
		MessageDigest md5 = MessageDigest.getInstance("MD5"); 
		BASE64Encoder base64en = new BASE64Encoder(); 
		// 加密后的字符串 
		String newstr = base64en.encode(md5.digest(str.getBytes("utf-8"))); 
		return newstr; 
	} 

	public boolean Checkuserinfo(){
		if(userName.equals(null) || userpass.equals(null) ||useremail.equals(null)||sex.equals(null)){
			return false;
		}
		return true;
	}
	
	/**
	 * 增加用户
	 */
	public String execute() throws Exception
	{
		
		if(Checkuserinfo()){
			System.out.println(Checkuserinfo());
			try {
				String regdate = cal.get(Calendar.YEAR)+"年"+cal.get(Calendar.MONTH)+"月"+cal.get(Calendar.DATE)+"日";
				userpass = EncoderPwdByMd5(userpass);
				System.out.println("user="+userName+";passwd="+userpass+";email="+useremail+";regdate="+regdate);
				user = new User();
				user.setUserName(userName);
				user.setUserpass(userpass);
				user.setUseremail(useremail);
				user.setSex(sex);
				user.setRegdate(regdate);
				if(userDAO.addUser(user) <= 0){
					return ERROR;
				}
			} catch (Exception e) {
				System.out.println(e.getMessage());
				return ERROR;
			}
			return SUCCESS;
		}
		else{
			System.out.println(Checkuserinfo());
			return ERROR;
		}
		
	}
	/**
	 * 
	  * CplUserInfo 方法 
	  * 方法说明:返回用户个人信息
	  * @return 
	  * @return String 
	  * @author Evan 
	  * @date 2012-12-23
	 */
	@SuppressWarnings("unchecked")
	public String CplUserInfo(){
		System.out.println("userId = "+userId);
		try {
			if(!(userDAO.findUserById(userId).equals(null))){
				ActionContext actionContext = ActionContext.getContext();
				Map session = actionContext.getSession();
				session.put("user", userDAO.findUserById(userId));
				return "cpluserinfo";
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
			return "exception";
		}
		
		return "exception";
	}
	/**
	 * 
	  * UpUserInfo 方法 
	  * 方法说明:完善用户信息 和修改用户信息
	  * @return 
	  * @return String 
	  * @author Evan 
	  * @date 2012-12-23
	 */
	public String UpUserInfo(){
		System.out.println("birthday = "+birthday+"headImage = "+headImage+"sex = "+sex+"usertype = "+usertype+"useremail = "+useremail+"regdate = "+regdate);
		try {
			user = new User();
			user.setBirthday(birthday);
			user.setHeadImage(headImage);
			user.setSex(sex);
			user.setUseremail(useremail);
			user.setUserName(userName);
			user.setUsertype(usertype);
			user.setRegdate(regdate);
			user.setUserId(userId);
			user.setUserpass(userpass);
			if(userDAO.updateUserInfo(user)){
				return "update";
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
		}
		return "error";
	}
	
	public String destoryUser(){
		try {
			this.getSession().remove("UserName");
			this.getSession().remove("Post");
			this.getSession().remove("PostList");
			this.getSession().remove("user");
			return "logout";
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return "logoutErr";
	}
	
	

	public String getRegdate() {
		return regdate;
	}
	public void setRegdate(String regdate) {
		this.regdate = regdate;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	public String getHeadImage() {
		return headImage;
	}
	public void setHeadImage(String headImage) {
		this.headImage = headImage;
	}
	public String getUsertype() {
		return usertype;
	}
	public void setUsertype(String usertype) {
		this.usertype = usertype;
	}	
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserpass() {
		return userpass;
	}
	public void setUserpass(String userpass) {
		this.userpass = userpass;
	}
	public String getUseremail() {
		return useremail;
	}
	public void setUseremail(String useremail) {
		this.useremail = useremail;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public User getUser() {
		return user;
	}

}
